package T3;

import java.util.Random;
import java.util.Scanner;

class Player {
    private String name;
    private int score;

    public Player(String name) {
        this.name = name;
        this.score = 0;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }

    public void increaseScore() {
        score++;
    }
}

class Game {
    private Player player;
    private Player computer;
    private Scanner scanner = new Scanner(System.in);
    private Random random = new Random();

    public Game() {
        System.out.println("请选择角色(1. 关羽，2. 刘备，3. 曹操)");
        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                player = new Player("关羽");
                break;
            case 2:
                player = new Player("刘备");
                break;
            case 3:
                player = new Player("曹操");
                break;
            default:
                System.out.println("无效选择，默认选择曹操");
                player = new Player("曹操");
        }
        computer = new Player("电脑");
    }


    private int getPlayerAction() {
        System.out.println("请出拳:(1.剪刀，2.石头，3.布)");
        return scanner.nextInt();
    }


    private int getComputerAction() {
        return random.nextInt(3) + 1;
    }


    private void compareActions(int playerAction, int computerAction) {
        if (playerAction == computerAction) {
            System.out.println("平局！");
        } else if ((playerAction == 1 && computerAction == 3) ||
                (playerAction == 2 && computerAction == 1) ||
                (playerAction == 3 && computerAction == 2)) {
            System.out.println("恭喜，您赢了！！！");
            player.increaseScore();
        } else {
            System.out.println("很遗憾，您输了！");
            computer.increaseScore();
        }
    }

    public void startGame() {
        int round = 0;
        while (true) {
            round++;
            int playerAction = getPlayerAction();
            int computerAction = getComputerAction();
            System.out.println("您出" + actionToString(playerAction));
            System.out.println("电脑出" + actionToString(computerAction));
            compareActions(playerAction, computerAction);
            System.out.println("本局对战结束:");
            System.out.println(player.getName() + ":" + player.getScore());
            System.out.println(computer.getName() + ":" + computer.getScore());
            System.out.println("输入y继续，n退出");
            String input = scanner.next();
            if ("n".equalsIgnoreCase(input)) {
                System.out.println("谢谢使用");
                System.out.println(player.getName() + " vs " + computer.getName());
                System.out.println("对战次数:" + round);
                if (player.getScore() > computer.getScore()) {
                    System.out.println("结果,您赢了！");
                } else if (player.getScore() < computer.getScore()) {
                    System.out.println("结果,输得很惨啊！！");
                } else {
                    System.out.println("结果,平局");
                }
                break;
            }
        }
    }

    private String actionToString(int action) {
        switch (action) {
            case 1:
                return "剪刀";
            case 2:
                return "石头";
            case 3:
                return "布";
            default:
                return "无效出拳";
        }
    }
}

class Main {
    public static void main(String[] args) {
        Game game = new Game();
        game.startGame();
    }
}